<?php
namespace Budget\BudgetBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Budget\BudgetBundle\Entity\Family;
use Budget\BudgetBundle\Entity\FamilyMonth;
use Budget\BudgetBundle\Helper\DateHelper;

class FamilyMonthRepository extends EntityRepository
{
    public function findByDateForFamily(\DateTime $date, Family $family)
    {
        $qb = $this->createQueryBuilder('fm')->select('fm, f')
            ->join('fm.family', 'f')
            ->where('fm.family = :familId')
            ->andWhere('fm.month = :month')
            ->andWhere('fm.year = :year')
            ->andWhere('fm.date_deleted IS NULL')
            ->setParameters(array(
                'familId' => $family->getId(),
                'month'   => $date->format('n'),
                'year'    => $date->format('Y')
            ));

        return $qb->getQuery()->getOneOrNullResult();
    }

    public function findPrevious(FamilyMonth $familyMonth)
    {
        $date = clone $familyMonth->getStart();
        $date->modify('-1 month');

        $qb = $this->createQueryBuilder('fm')->select('fm, f')
            ->join('fm.family', 'f')
            ->where('fm.family = :familId')
            ->andWhere('fm.month = :month')
            ->andWhere('fm.year = :year')
            ->andWhere('fm.date_deleted IS NULL')
            ->setParameters(array(
                'familId' => $familyMonth->getFamily()->getId(),
                'month'   => $date->format('n'),
                'year'    => $date->format('Y')
            ));

        return $qb->getQuery()->getOneOrNullResult();
    }

    public function findByIdForFamily(Family $family, $id)
    {
        $qb = $this->createQueryBuilder('fm')->select('fm, f')
            ->join('fm.family', 'f')
            ->where('fm.family = :familId')
            ->andWhere('fm.id = :id')
            ->andWhere('fm.date_deleted IS NULL')
            ->setParameters(array(
                'familId' => $family->getId(),
                'id'      => $id
            ));
        //echo $qb->getQuery()->getSQL();exit;
        return $qb->getQuery()->getOneOrNullResult();
    }

    /**
     * Returns the current month defined by the family
     *
     * @param Family $family
     * @return mixed
     * @throws \Doctrine\ORM\NonUniqueResultException
     */
    public function findCurrentForFamily(Family $family)
    {
        $date = new \DateTime();

        $qb = $this->createQueryBuilder('fm')->select('fm, f')
            ->join('fm.family', 'f')
            ->where('fm.family = :familyId')
            ->andWhere(':date BETWEEN fm.start AND fm.end')
            ->andWhere('fm.date_deleted IS NULL')
            ->setParameters(array(
                'familyId' => $family->getId(),
                'date'     => $date->format(DateHelper::FORMAT_DATE_DB),
            ));

        return $qb->getQuery()->getOneOrNullResult();
    }

    public function findByParamsForFamily(Family $family, $params)
    {
        $qb = $this->createQueryBuilder('fm')->select('fm')
            ->where('fm.year = :year')
            ->andWhere('fm.month IN (:months)')
            ->andWhere('fm.family = :familyId')
            ->setParameters(array(
                'year'     => $params['year'],
                'months'   => $params['months'],
                'familyId' => $family->getId()
            ));

        return $qb->getQuery()->getResult();
    }
}
